package cn.tedu.egweibo3.controller;

import cn.tedu.egweibo3.common.response.JsonResult;
import cn.tedu.egweibo3.common.response.StatusCode;
import cn.tedu.egweibo3.mapper.UserMapper;
import cn.tedu.egweibo3.pojo.dto.UserLoginDTO;
import cn.tedu.egweibo3.pojo.dto.UserRegDTO;
import cn.tedu.egweibo3.pojo.entity.User;
import cn.tedu.egweibo3.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/v1/users/")
@Api(tags = "01.用户模块")
@Slf4j
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 1.注册功能
     * 接口地址: /v1/users/reg
     * 请求方式: POST
     * 请求数据类型: application/json
     * 响应数据类型: -*-
     */
    @PostMapping("reg")
    @ApiOperation("用户注册功能")
    public JsonResult reg(@RequestBody UserRegDTO userRegDTO) {
        log.debug("userRegDTO = " + userRegDTO);
        UserVO userVO = userMapper.selectByUsername(userRegDTO.getUsername());
        if (userVO != null) {
            return new JsonResult(StatusCode.USERNAME_ALREADY_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegDTO, user);
        user.setCreated(new Date());
        userMapper.insert(user);
        return JsonResult.ok();
    }

    /**
     * 2.登录功能
     * 接口地址: /v1/users/login
     * 请求方式: POST
     * 请求数据类型: application/json
     * 响应数据类型: -*-
     */
    @PostMapping("login")
    @ApiOperation("登录功能")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session) {
        log.debug("userLoginDTO = " + userLoginDTO);
        UserVO userVO = userMapper.selectByUsername(userLoginDTO.getUsername());
        if (userVO == null) {
            return new JsonResult(StatusCode.USERNAME_ERROR);
        }
        if (!userLoginDTO.getPassword().equals(userVO.getPassword())) {
            return new JsonResult(StatusCode.PASSWORD_ERROR);
        }
        session.setAttribute("user", userVO);
        return JsonResult.ok();
    }

    /**
     * 3.获取当前用户
     * 接口地址: /v1/users/currentUser
     * 请求方式: GET
     * 请求数据类型: application/x-www-form-urlenstated
     * 响应数据类型: -*-
     */
    @GetMapping("currentUser")
    @ApiOperation("获取当前用户功能")
    public JsonResult currentUser(@ApiIgnore HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        return JsonResult.ok(userVO);
    }

    /**
     * 4.退出登录
     * 接口地址: /v1/users/logout
     * 请求方式: GET
     * 请求数据类型: application/x-www-form-urlenstated
     * 响应数据类型: -*-
     */
    @GetMapping("logout")
    @ApiOperation("退出登录功能")
    public void logout (@ApiIgnore HttpSession session){
        session.removeAttribute("user");
    }
}
